package anatex.domain;

import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import anatex.domain.DocumentDomain;
import anatex.domain.Task.EnumState;

import javax.persistence.EntityManager;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

@RooJavaBean
@RooToString
@RooEntity(finders = { "findDocumentDomainTasksByCurrentStateAndEnabled" })
public class DocumentDomainTask {

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "SS")
    private Date startTime;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "SS")
    private Date finishTime;

    private Boolean enabled;

    public enum DomainTaskEnumState {

        NOT_STARTED, WORKING, FINISHED
    };

    private DomainTaskEnumState currentState;

    @ManyToOne
    private UserDocumentDomain userDocumentDomain;
    
    public void setCurrentState(DomainTaskEnumState currentState) {
        this.currentState = currentState;
    }
    
    public static TypedQuery<DocumentDomainTask> findDocumentDomainTasksByCurrentStateAndEnabled(DomainTaskEnumState currentState, Boolean enabled) {
        if (currentState == null) throw new IllegalArgumentException("The currentState argument is required");
        if (enabled == null) throw new IllegalArgumentException("The enabled argument is required");
        EntityManager em = DocumentDomainTask.entityManager();
        TypedQuery<DocumentDomainTask> q = em.createQuery("SELECT DocumentDomainTask FROM DocumentDomainTask AS documentdomaintask WHERE documentdomaintask.currentState = :currentState AND documentdomaintask.enabled = :enabled", DocumentDomainTask.class);
        q.setParameter("currentState", currentState);
        q.setParameter("enabled", enabled);
        return q;
    }
}
